<?php
namespace Futures\Controller;
use Futures\Model\OptionModel;
/**
 *  批处理做市商资金流水
 */
class UpdateMakerLogController{

   //批处理插入 
   public function insert_maker_log(){
        //插入螺纹钢订单流水
        $this->insert_forder_log();
        //插入恒指订单流水
        $this->insert_morder_log();

        //插入欧元订单流水
        $this->insert_eorder_log();

        //插入充值流水（包含手续费）
        $this->insert_recharge_log();
        //插入提现流水（包含手续费）
        $this->insert_withdrawals_log();
        //插入佣金流水
        $this->insert_commission_log();
       
   }

   public function insert_forder_log(){
        //futures_order 和 mongodb_order 添加 has_maker_log
        $orders = M('FuturesOrder')->where(array('has_maker_log'=>0,'status'=>2))->select();
        $Mongo= new OptionModel('maker_log');//集合名（表名）
        foreach($orders as $o){
            //获取做市商的做市比例
            $user =  M('Follow')->where(array('id'=>$o['user_id']))->find();   //找到订单用户
            if($user['maker_id']<=0) continue;
            $where['user_id'] = $user['maker_id'];
            $where['user_id'] = array('gt',2);
            $maker = M('FuturesAgent')->where($where)->find();  //找到做市商
            $market = ($user['maker_id']>2&&$maker['market']>0)?$maker['market']:0;
            //$market_money=$o['total_price']*0.98*$market;
            $water = ($maker['water']>0)?$maker['water']:0.02;
            $market_money = $o['total_price']*(1-$water)*$market;
            if($user['maker_id']>2 && $maker && $market>0){
                $datas[0]=array(
                    'maker_id'=>intval($user['maker_id']),   //做市商id
                    'user_id'=>intval($o['user_id']),     //购买用户
                    'money'=>$market_money,   //下单操作金额
                    'money_type'=>1,              //1是下单
                    'option_id'=>intval($o['id']),                  //订单id
                    'order_id_type'=>1, //1是螺纹钢订单
                    'option_time'=>$o['create_time'],                 
                    'note'=>'下单入金',            
                    'create_time'=>time(),                     
                );

            }
            
            //默认做市商比例
            $default_market_money=$o['total_price']*(1-$water)-$market_money;
            $datas[1]=array(
                'maker_id'=>1,   //做市商id
                'user_id'=>intval($o['user_id']),     //购买用户
                'money'=>$default_market_money,   //下单操作金额
                'money_type'=>1,              //1是下单
                'option_id'=>intval($o['id']),                  //订单id
                'order_id_type'=>1, //1是螺纹钢订单
                'option_time'=>$o['create_time'],                 //平仓买价
                'note'=>'下单入金',            //0-看跌 1-看涨
                'create_time'=>time(),                     //盈利
            );
            //极客部落
            $datas[2]=array(
                'maker_id'=>2,   //做市商id
                'user_id'=>intval($o['user_id']),     //购买用户
                'money'=>$o['total_price']*$water,   //下单操作金额
                'money_type'=>1,              //1是下单
                'option_id'=>intval($o['id']),                  //订单id
                'order_id_type'=>1, //1是螺纹钢订单
                'option_time'=>$o['create_time'],                 //平仓买价
                'note'=>'下单入金',            //0-看跌 1-看涨
                'create_time'=>time(),                     //盈利
            );
            //如果订单有盈利
            if($o['profit']>0){
                if($user['maker_id']>2 && $maker && $market>=0) {
                    $datas[3]=array(
                        'maker_id'=>intval($user['maker_id']),   //做市商id
                        'user_id'=>intval($o['user_id']),     //购买用户
                        'money'=>$o['profit']*$market,   //下单操作金额
                        'money_type'=>5,              //5是盈利退回
                        'option_id'=>intval($o['id']),                  //订单id
                        'order_id_type'=>1, //1是螺纹钢订单
                        'option_time'=>$o['create_time'],                 
                        'note'=>'下单盈利退回',            
                        'create_time'=>time(),                     
                    );
                }
                $datas[4]=array(
                    'maker_id'=>1,   //做市商id
                    'user_id'=>intval($o['user_id']),     //购买用户
                    'money'=>$o['profit']*(1-$market),   //下单操作金额
                    'money_type'=>5,              //5是盈利退回
                    'option_id'=>intval($o['id']),                  //订单id
                    'order_id_type'=>1, //1是螺纹钢订单
                    'option_time'=>$o['create_time'],                 
                    'note'=>'下单盈利退回',            
                    'create_time'=>time(),                     
                );
            }

            $res = $Mongo->addAll($datas);
            if($res){
                M('FuturesOrder')->where('id='.$o['id'])->save(array('has_maker_log'=>1));
            }
        }
   }

   public function insert_morder_log(){
        $Mongo= new OptionModel('maker_log');//集合名（表名）
        //futures_order 和 mongodb_order 添加 has_maker_log
        $M= new OptionModel('order');//集合名（表名）
        $orders = $M->where(array('has_maker_log'=>0,'status'=>2))->select();   //取出mongodb 中的订单
        //dump($orders);
        foreach($orders as $o){
            //获取做市商的做市比例
            $user =  M('Follow')->where(array('id'=>$o['user_id']))->find();   //找到订单用户
            if($user['maker_id']<=0) continue;
            $where['user_id'] = $user['maker_id'];
            $where['user_id'] = array('gt',2);
            $maker = M('FuturesAgent')->where($where)->find();  //找到做市商
            $market = ($user['maker_id']>2&&$maker['market'])?$maker['market']:0;
            $water = ($maker['water']>0)?$maker['water']:0.02;
            $market_money = $o['invest']*(1-$water)*$market;
            if($user['maker_id']>2 && $maker && $market>0){
                $datas[0]=array(
                    'maker_id'=>intval($user['maker_id']),   //做市商id
                    'user_id'=>intval($o['user_id']),     //购买用户
                    'money'=>$market_money,   //下单操作金额
                    'money_type'=>1,              //1是下单
                    'option_id'=>intval($o['id']),                  //订单id
                    'order_id_type'=>2, //2是恒指
                    'option_time'=>$o['start_time'],                 
                    'note'=>'恒指下单入金',            
                    'create_time'=>time(),                     
                );

            }
            //默认做市商比例
            //$default_market_money=$o['invest']*0.98-$market_money;
            $default_market_money=$o['invest']*(1-$water)-$market_money;
            $datas[1]=array(
                'maker_id'=>1,   //做市商id
                'user_id'=>intval($o['user_id']),     //购买用户
                'money'=>$default_market_money,   //下单操作金额
                'money_type'=>1,              //1是下单
                'option_id'=>intval($o['id']),                  //订单id
                'order_id_type'=>2, //1是螺纹钢订单
                'option_time'=>$o['start_time'],                 //平仓买价
                'note'=>'恒指下单入金',            //0-看跌 1-看涨
                'create_time'=>time(),                     //盈利
            );
            //极客部落
            $datas[2]=array(
                'maker_id'=>2,   //做市商id
                'user_id'=>intval($o['user_id']),     //购买用户
                'money'=>$o['invest']*$water,   //下单操作金额
                'money_type'=>1,              //1是下单
                'option_id'=>intval($o['id']),                  //订单id
                'order_id_type'=>2, //1是螺纹钢订单
                'option_time'=>$o['start_time'],                 //平仓买价
                'note'=>'恒指下单入金',            //0-看跌 1-看涨
                'create_time'=>time(),                     //盈利
            );
            //如果订单有盈利
            if($o['profit']>0){
                if($o['profit']==0) $o['profit']=$o['invest'];
                if($user['maker_id']>2 && $maker && $market>=0) {
                    $datas[3]=array(
                        'maker_id'=>intval($user['maker_id']),   //做市商id
                        'user_id'=>intval($o['user_id']),     //购买用户
                        'money'=>$o['profit']*$market,   //下单操作金额
                        'money_type'=>5,              //5是盈利退回
                        'option_id'=>intval($o['id']),                  //订单id
                        'order_id_type'=>2, //1是螺纹钢订单
                        'option_time'=>$o['start_time'],                 
                        'note'=>'恒指下单盈利退回',            
                        'create_time'=>time(),                     
                    );
                }
                $datas[4]=array(
                    'maker_id'=>1,   //做市商id
                    'user_id'=>intval($o['user_id']),     //购买用户
                    'money'=>$o['profit']*(1-$market),   //下单操作金额
                    'money_type'=>5,              //5是盈利退回
                    'option_id'=>intval($o['id']),                  //订单id
                    'order_id_type'=>2, //1是螺纹钢订单
                    'option_time'=>$o['start_time'],                 
                    'note'=>'恒指下单盈利退回',            
                    'create_time'=>time(),                     
                );
            }

            $res = $Mongo->addAll($datas);
            if($res){
                $M->where('id='.$o['id'])->save(array('has_maker_log'=>1));
            }
        }
    }

    public function insert_eorder_log(){
        $Mongo= new OptionModel('maker_log');//集合名（表名）
        //futures_order 和 mongodb_order 添加 has_maker_log
        $M= new OptionModel('euro_order');//集合名（表名）
        $orders = $M->where(array('has_maker_log'=>0,'status'=>2))->select();   //取出mongodb 中的订单
        //dump($orders);
        foreach($orders as $o){
            //获取做市商的做市比例
            $user =  M('Follow')->where(array('id'=>$o['user_id']))->find();   //找到订单用户
            if($user['maker_id']<=0) continue;
            $where['user_id'] = $user['maker_id'];
            $where['user_id'] = array('gt',2);
            $maker = M('FuturesAgent')->where($where)->find();  //找到做市商
            $market = ($user['maker_id']>2&&$maker['market'])?$maker['market']:0;
            $water = ($maker['water']>0)?$maker['water']:0.02;
            $market_money = $o['invest']*(1-$water)*$market;
            if($user['maker_id']>2 && $maker && $market>0){
                $datas[0]=array(
                    'maker_id'=>intval($user['maker_id']),   //做市商id
                    'user_id'=>intval($o['user_id']),     //购买用户
                    'money'=>$market_money,   //下单操作金额
                    'money_type'=>1,              //1是下单
                    'option_id'=>intval($o['id']),                  //订单id
                    'order_id_type'=>3, //2是恒指
                    'option_time'=>$o['start_time'],                 
                    'note'=>'欧元下单入金',            
                    'create_time'=>time(),                     
                );

            }
            //默认做市商比例
            //$default_market_money=$o['invest']*0.98-$market_money;
            $default_market_money=$o['invest']*(1-$water)-$market_money;
            $datas[1]=array(
                'maker_id'=>1,   //做市商id
                'user_id'=>intval($o['user_id']),     //购买用户
                'money'=>$default_market_money,   //下单操作金额
                'money_type'=>1,              //1是下单
                'option_id'=>intval($o['id']),                  //订单id
                'order_id_type'=>3, //1是螺纹钢订单
                'option_time'=>$o['start_time'],                 //平仓买价
                'note'=>'欧元下单入金',            //0-看跌 1-看涨
                'create_time'=>time(),                     //盈利
            );
            //极客部落
            $datas[2]=array(
                'maker_id'=>2,   //做市商id
                'user_id'=>intval($o['user_id']),     //购买用户
                'money'=>$o['invest']*$water,   //下单操作金额
                'money_type'=>1,              //1是下单
                'option_id'=>intval($o['id']),                  //订单id
                'order_id_type'=>3, //1是螺纹钢订单
                'option_time'=>$o['start_time'],                 //平仓买价
                'note'=>'欧元下单入金',            //0-看跌 1-看涨
                'create_time'=>time(),                     //盈利
            );
            //如果订单有盈利
            if($o['profit']>0){
                if($o['profit']==0) $o['profit']=$o['invest'];
                if($user['maker_id']>2 && $maker && $market>=0) {
                    $datas[3]=array(
                        'maker_id'=>intval($user['maker_id']),   //做市商id
                        'user_id'=>intval($o['user_id']),     //购买用户
                        'money'=>$o['profit']*$market,   //下单操作金额
                        'money_type'=>5,              //5是盈利退回
                        'option_id'=>intval($o['id']),                  //订单id
                        'order_id_type'=>3, //1是螺纹钢订单
                        'option_time'=>$o['start_time'],                 
                        'note'=>'欧元下单盈利退回',            
                        'create_time'=>time(),                     
                    );
                }
                $datas[4]=array(
                    'maker_id'=>1,   //做市商id
                    'user_id'=>intval($o['user_id']),     //购买用户
                    'money'=>$o['profit']*(1-$market),   //下单操作金额
                    'money_type'=>5,              //5是盈利退回
                    'option_id'=>intval($o['id']),                  //订单id
                    'order_id_type'=>3, //1是螺纹钢订单
                    'option_time'=>$o['start_time'],                 
                    'note'=>'欧元下单盈利退回',            
                    'create_time'=>time(),                     
                );
            }

            $res = $Mongo->addAll($datas);
            if($res){
                $M->where('id='.$o['id'])->save(array('has_maker_log'=>1));
            }
        }
    }

    public function insert_recharge_log(){
        //futures_recharge
        $orders = M('FuturesRecharge')->where(array('has_maker_log'=>0,'is_pay'=>1))->select();
        $Mongo= new OptionModel('maker_log');//集合名（表名）
        foreach($orders as $o){
            //获取做市商的做市比例
            $user =  M('Follow')->where(array('id'=>$o['user_id']))->find();   //找到订单用户
            if($user['maker_id']<=0) continue;
            $datas[0]=array(
                'maker_id'=>intval($user['maker_id']),   //做市商id
                'user_id'=>intval($o['user_id']),     //购买用户
                'money'=>$o['money'],   //下单操作金额
                'money_type'=>2,              //1是下单
                'option_id'=>intval($o['id']),                  //订单id
                'order_id_type'=>0,
                'option_time'=>$o['create_time'],                 
                'note'=>'充值入金',            
                'create_time'=>time(),                     
            );
            $datas[1]=array(
                'maker_id'=>intval($user['maker_id']),   //做市商id
                'user_id'=>intval($o['user_id']),     //购买用户
                'money'=>$o['money']*0.006,   //下单操作金额
                'money_type'=>6,              //1是下单
                'option_id'=>intval($o['id']),                  //订单id
                'order_id_type'=>0,
                'option_time'=>$o['create_time'],                 
                'note'=>'充值入金手续费',            
                'create_time'=>time(),                     
            );
            $res = $Mongo->addAll($datas);
            if($res){
                M('FuturesRecharge')->where('id='.$o['id'])->save(array('has_maker_log'=>1));
            }
        }
    }


    public function insert_withdrawals_log(){
        //wp_futures_withdrawals 加入has_maker_log字段
        $orders = M('FuturesWithdrawals')->where(array('has_maker_log'=>0,'is_pay'=>1))->select();
        $Mongo= new OptionModel('maker_log');//集合名（表名）
        foreach($orders as $o){
            //获取做市商的做市比例
            $user =  M('Follow')->where(array('id'=>$o['user_id']))->find();   //找到订单用户
            if($user['maker_id']<=0) continue;
            $datas[0]=array(
                'maker_id'=>intval($user['maker_id']),  
                'user_id'=>intval($o['user_id']),     
                'money'=>$o['money'],   
                'money_type'=>3,              
                'option_id'=>intval($o['id']),                  
                'order_id_type'=>0,
                'option_time'=>$o['pay_time'],                 
                'note'=>'提现出金',            
                'create_time'=>time(),                     
            );
            //手续费计算
            $x_money = $o['money']*0.01;
            if($x_money<2) $x_money=2;
            $datas[1]=array(
                'maker_id'=>intval($user['maker_id']),   //做市商id
                'user_id'=>intval($o['user_id']),     //购买用户
                'money'=>$x_money,   //下单操作金额
                'money_type'=>7,              //1是下单
                'option_id'=>intval($o['id']),                  //订单id
                'order_id_type'=>0,
                'option_time'=>$o['pay_time'],                 
                'note'=>'提现手续费',            
                'create_time'=>time(),                     
            );

            $res = $Mongo->addAll($datas);
            if($res){
                M('FuturesWithdrawals')->where('id='.$o['id'])->save(array('has_maker_log'=>1));
            }
        }
    }

    public function insert_commission_log(){
        //wp_futures_withdrawals 加入has_maker_log字段
        $orders = M('FuturesCommission')->where(array('has_maker_log'=>0,'status'=>1))->select();
        $Mongo= new OptionModel('maker_log');//集合名（表名）
        foreach($orders as $o){           
            $user =  M('Follow')->where(array('id'=>$o[' commission_user_id']))->find();
            if($user['maker_id']<=0) continue;
            $maker = M('FuturesAgent')->where(array('user_id'=>$user['maker_id']))->find();  //找到做市商
            $datas[0]=array(
                'maker_id'=>intval($user['maker_id']),  
                'user_id'=>intval($o['commission_user_id']),     
                'money'=>$o['commission'],   //佣金
                'money_type'=>4,              
                'option_id'=>intval($o['id']),                  
                'order_id_type'=>0,
                'option_time'=>$o['update_time'],                 
                'note'=>'佣金',            
                'create_time'=>time(),                     
            );
            $res = $Mongo->addAll($datas);
            if($res){
                M('FuturesCommission')->where('id='.$o['id'])->save(array('has_maker_log'=>1));
            }
        }
    }

    public function test(){
         //$M= new OptionModel('maker_log');//集合名（表名
         $M= new OptionModel('order');
         $M->where(1)->save(array('type'=>1));
         //$M->where(1)->save(array('maker_id'=>1));
    }

}